﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;
using CE.Security;

/// <summary>
/// Summary description for BrokerProfileDB
/// </summary>
namespace CE.User
{
    public class BrokerProfileDB
    {
        public BrokerProfileDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(BrokerProfile brokerprofile)
        {
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocBrokerProfileInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProfileID", brokerprofile.ProfileId);
            _sPara.Add("@Salutation", brokerprofile.Salutation);
            _sPara.Add("@FirstName", brokerprofile.FirstName);
            _sPara.Add("@SurName", brokerprofile.SurName);
            _sPara.Add("@DOB", brokerprofile.DOB);
            _sPara.Add("@Gender", brokerprofile.Gender);
            _sPara.Add("@MaritalStatus", brokerprofile.MaritalStatus);
            _sPara.Add("@OccupationType", brokerprofile.OccupationType);
            _sPara.Add("@Occupation", brokerprofile.Occupation);
            _sPara.Add("@IDNOType", brokerprofile.IDType);
            _sPara.Add("@IDNO", brokerprofile.IDNo);
            _sPara.Add("@Nationality", brokerprofile.Nationality);
            _sPara.Add("@MontlyIncome", brokerprofile.MontlyIncome);
            _sPara.Add("@OtherIncome", brokerprofile.OtherIncome);
            _sPara.Add("@JobNature", brokerprofile.JobNature);
            _sPara.Add("@YearDriveSg", brokerprofile.YearDriveSg);
            _sPara.Add("@Status", brokerprofile.Status);
            _sPara.Add("@RefCEUserId", brokerprofile.RefCEUserId);
            _sPara.Add("@Info1", brokerprofile.Info1);
            _sPara.Add("@Info2", brokerprofile.Info2);
            _sPara.Add("@Info3", brokerprofile.Info3);
            _sPara.Add("@Info4", brokerprofile.Info4);
            _sPara.Add("@Info5", brokerprofile.Info5);
            _sPara.Add("@Info6", brokerprofile.Info6);
            _sPara.Add("@Info7", brokerprofile.Info7);
            _sPara.Add("@Info8", brokerprofile.Info8);
            _sPara.Add("@Info9", brokerprofile.Info9);
            _sPara.Add("@Info10", brokerprofile.Info10);
            _sPara.Add("@Info11", brokerprofile.Info11);
            _sPara.Add("@Info12", brokerprofile.Info12);
            _sPara.Add("@Info13", brokerprofile.Info13);
            _sPara.Add("@Info14", brokerprofile.Info14);
            _sPara.Add("@Info15", brokerprofile.Info15);
            _sPara.Add("@Info16", brokerprofile.Info16);
            _sPara.Add("@Info17", brokerprofile.Info17);
            _sPara.Add("@Info18", brokerprofile.Info18);
            _sPara.Add("@Info19", brokerprofile.Info19);
            _sPara.Add("@Info20", brokerprofile.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public BrokerProfile GetProfile(Int64 RefCEUserId)
        {
            MyDatabase _Db = new MyDatabase("GGR_DBConnectionString");
            string _spName = "sprocBrokerProfileSelectSingleItem";
            BrokerProfile _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@RefCEUserId", RefCEUserId);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new BrokerProfile();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }

        private BrokerProfile FillDataRecord(DataTable myDataTable)
        {
            BrokerProfile myProfile = new BrokerProfile();
            myProfile.ProfileId = Convert.ToInt64(myDataTable.Rows[0]["ProfileId"].ToString());
            myProfile.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myProfile.Salutation = myDataTable.Rows[0]["Salutation"].ToString();
            string _firstname = myDataTable.Rows[0]["FirstName"].ToString();
            if (_firstname != string.Empty)
            {
                myProfile.FirstName = _firstname;
            }
            string _surname = myDataTable.Rows[0]["SurName"].ToString();
            if (_surname != string.Empty)
            {
                myProfile.SurName = _surname;
            }
            myProfile.DOB = Convert.ToDateTime(myDataTable.Rows[0]["DOB"].ToString());
            myProfile.Gender = Convert.ToChar(myDataTable.Rows[0]["Gender"].ToString());
            myProfile.MaritalStatus = Convert.ToInt32(myDataTable.Rows[0]["MaritalStatus"].ToString());
            myProfile.OccupationType = Convert.ToInt32(myDataTable.Rows[0]["OccupationType"].ToString());
            myProfile.Occupation = Convert.ToInt32(myDataTable.Rows[0]["Occupation"].ToString());
            string _idtype = myDataTable.Rows[0]["IDNoType"].ToString();
            if (_idtype != string.Empty)
            {
                myProfile.IDType = _idtype;
            }
            string _idno = myDataTable.Rows[0]["IDNo"].ToString();
            if (_idno != string.Empty)
            {
                myProfile.IDNo = _idno;
            }
            string _nationality = myDataTable.Rows[0]["Nationality"].ToString();
            if (_nationality != string.Empty)
            {
                myProfile.Nationality = _nationality;
            }
            myProfile.MontlyIncome = Convert.ToSingle(myDataTable.Rows[0]["MontlyIncome"].ToString());
            myProfile.OtherIncome = Convert.ToSingle(myDataTable.Rows[0]["OtherIncome"].ToString());
            myProfile.JobNature = myDataTable.Rows[0]["JobNature"].ToString();
            myProfile.YearDriveSg = Convert.ToInt16(myDataTable.Rows[0]["YearDriveSg"].ToString());
            myProfile.Status = myDataTable.Rows[0]["Status"].ToString();
            if (myDataTable.Rows[0]["Info1"] != DBNull.Value)
            {
                myProfile.Info1 = myDataTable.Rows[0]["Info1"].ToString();
            }
            if (myDataTable.Rows[0]["Info2"] != DBNull.Value)
            {
                myProfile.Info2 = myDataTable.Rows[0]["Info2"].ToString();
            }
            if (myDataTable.Rows[0]["Info3"] != DBNull.Value)
            {
                myProfile.Info3 = myDataTable.Rows[0]["Info3"].ToString();
            }
            return myProfile;
        }

    }
}